<template>
  <el-container class="container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="">
        <span>电商管理系统</span>
      </div>
      <el-button class="logout" type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="collapse ? '64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="collapse"
          :collapse-transition="false"
          router
          :default-active="activePath">
          <!--  一级菜单  -->
          <el-submenu v-for="item in menulist" :index="item.id+''" :key="item.id">
            <!--  一级菜单的模版区域 -->
            <template slot="title">
              <!--  图标  -->
              <i :class="iconsObj[item.id]"></i>
              <!--  文本  -->
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item v-for="it in item.children" :index="'/'+it.path" :key="it.id" @click="saveNameState(it.path)">
              <template slot="title">
                <!--  图标  -->
                <i class="el-icon-menu"></i>
                <!--  文本  -->
                <span>{{it.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!--  路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
  <!--    <el-button type="info" @click="logout">退出</el-button>-->

</template>

<script>
  export default {
    data() {
      return {
        menulist: [],
        iconsObj: {
          '125': 'iconfont icon-user',
          '103': 'iconfont icon-tijikongjian',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-danju',
          '145': 'iconfont icon-baobiao'
        },
        collapse: false,
        activePath: ''
      }
    },
    created() {
      this.getMenuList()
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
      logout() {
        window.sessionStorage.clear();
        this.$router.push('./login');
      },
      async getMenuList() {
        const {data} = await this.$http.get('menus');
        console.log(data);
        if (data.meta.status === 200) {
          this.menulist = data.data
        } else {
          this.$message.error(data.meta.msg);
        }
      },
      toggleCollapse() {
        this.collapse = !this.collapse;
      },
      saveNameState(path) {
        window.sessionStorage.setItem('activePath', '/' + path)
        this.activePath = '/' + path
      }
    }
  }
</script>

<style lang="less" scoped>
  .container {
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
    font-size: 1.5em;
    display: flex;
    justify-content: space-between;
    color: white;

    > div {
      padding-left: 0;
    }

    img {
      height: 40px;
      width: 40px;
    }
  }

  .el-aside {
    background-color: #3a3f4d;

    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background-color: #edf0f4;
  }

  .toggle-button {
    background-color: #2b4b6b;
    color: #fff;
    text-align: center;
    font-size: 10px;
    line-height: 24px;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
